clear all;

% top view region of AVM image
Region_TL_X = 21;
Region_TL_Y = 8;
Region_BR_X = 339;
Region_BR_Y = 466;

IMG_Height = 459;
IMG_Width = 319;

% Each camera's position in the top view image (cropped image)
F_CAM_X = 160;  F_CAM_Y = 80;
L_CAM_X = 113;  L_CAM_Y = 161;
R_CAM_X = 208;  R_CAM_Y = 161;
B_CAM_X = 160;  B_CAM_Y = 324;

load('PH_LUT_20140120.mat');

for i = 3 : 497
    % Load two successive image
    cur_img_name = ['DB\', int2str(i), '.jpg'];         cur_img = imread(cur_img_name);
    pre_img_name = ['DB\', int2str(i-2), '.jpg'];       pre_img = imread(pre_img_name);
    
    % Crop image
    cur_crop_img = cur_img(Region_TL_Y:Region_BR_Y, Region_TL_X:Region_BR_X);
    pre_crop_img = pre_img(Region_TL_Y:Region_BR_Y, Region_TL_X:Region_BR_X);
    
    % Frame Difference
    diff_img = abs(cur_crop_img - pre_crop_img);
    
    % Line Drawing @ Front
    for y = 1 : F_CAM_Y
        for line_index = 1 : 15
            cur_crop_img(y, Front_LUT(y,line_index)) = 255;
        end
    end
    for x = 1 : F_CAM_X
        for line_index = 1 : 5
            cur_crop_img(FL_Corner_LUT(x,line_index), x) = 255;
        end
    end
    for x = F_CAM_X : IMG_Width
        for line_index = 1 : 5
            cur_crop_img(FR_Corner_LUT(IMG_Width-x+1,line_index), x) = 255;
        end
    end
    
    % Line Drawing @ Back
    for y = B_CAM_Y : IMG_Height
        for line_index = 1 : 15
            cur_crop_img(y, Back_LUT(IMG_Height-y+1,line_index)) = 255;
        end
    end
    for x = 1 : B_CAM_X
        for line_index = 1 : 5
            cur_crop_img(BL_Corner_LUT(x,line_index), x) = 255;
        end
    end
    for x = B_CAM_X : IMG_Width
        for line_index = 1 : 5
            cur_crop_img(BR_Corner_LUT(IMG_Width-x+1,line_index), x) = 255;
        end
    end

    % Line Drawing @ Left
    for x = 1 : L_CAM_X
        for line_index = 1 : 19
            cur_crop_img(Left_LUT(x,line_index), x) = 255;
        end
    end

    % Line Drawing @ Right
    for x = R_CAM_X : IMG_Width
        for line_index = 1 : 19
            cur_crop_img(Right_LUT(IMG_Width-x+1,line_index), x) = 255;
        end
    end
    
    imshow(cur_crop_img); axis on; title(int2str(i));
    pause(0.1); 
end
